package com.sc.nft.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.controller.request.LotteryRequest;
import com.sc.nft.dao.ActivityInfoDao;
import com.sc.nft.entity.ActivityInfo;
import com.sc.nft.entity.IsJoinFrisbeeActivityVO;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.ActivityInfoService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 轮盘抽奖
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/roulette")
@Api(tags ="轮盘抽奖")
public class RouletteDrawController {

    private final ActivityInfoService activityInfoService;
    private final ActivityInfoDao activityInfoDao;
    private final Redisson redisson;

    @ApiOperation(value = "获取轮盘抽奖详情", notes = "获取轮盘抽奖详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getRouletteDrawDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<RouletteDrawDetailsVO> getBlindBoxDetails()  {
        return Result.success(activityInfoService.getBlindBoxDetails());
    }

    @ApiOperation(value = "抽奖", notes = "抽奖", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/lottery", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<LotteryVO> lottery(@Valid @RequestBody LotteryRequest request) throws GlobalException {
        RLock lock = redisson.getLock(RedisKeyEnum.LOTTERY_LOCK.getKey()+request.getId());
        LotteryVO lotteryVO =null;
        try{
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (res) {
                 lotteryVO = activityInfoService.lotteryById(request.getId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("当前人数过多，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success(lotteryVO);
    }


    @ApiOperation(value = "滚动记录", notes = "滚动记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getRollingRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<LotteryRecordListVO>> getRollingRecord()  {
        return Result.success(activityInfoService.getRollingRecord());
    }
    @ApiOperation(value = "抽奖记录", notes = "抽奖记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getLotteryRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ArrayList<LotteryRecordVO>> getLotteryRecord()  {
        return Result.success(activityInfoService.getLotteryRecord());
    }


    @ApiOperation(value = "获取飞盘活动商品", notes = "获取飞盘活动商品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getFrisbeeActivityPorudct", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<FrisbeeActivityPorudctVO> getFrisbeeActivityPorudct()  {
        return Result.success(activityInfoService.getFrisbeeActivityPorudct());
    }


    @ApiOperation(value = "是否可以参与分盘活动", notes = "是否可以参与分盘活动", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/isJoinFrisbeeActivity", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin(required = false)
    public Result<IsJoinFrisbeeActivityVO> isJoinFrisbeeActivityByUser()  {
        return Result.success(activityInfoService.isJoinFrisbeeActivityByUser(UserPool.getUser()));
    }
}
